﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BL;
using DA;

namespace StoreSystem
{
    public partial class CategoryForm : System.Web.UI.Page
    {
        DataManager bl = new DataManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string categoryId = Request.QueryString.Get("CategoryId");
                if (!string.IsNullOrEmpty(categoryId))
                {

                    Categories category = bl.GetCategory(int.Parse(categoryId));
                    txtCategoryName.Text = category.Name;
                }
            }
        }
        protected void btn_click(object sender, EventArgs e)
        {
            string categoryId = Request.QueryString.Get("CategoryId");
            if (!string.IsNullOrEmpty(categoryId))  //Edit Category
            {
                bool isAdded;
                isAdded = bl.EditCategory(int.Parse(categoryId), txtCategoryName.Text);
                if (isAdded)
                {
                    lblAdded.Visible = true;
                    Response.Redirect("CategoriesList.aspx", true);
                }
            }

            else
            {//new Category
                Categories Category = new Categories();
                Category.Name = txtCategoryName.Text;
                bool isAdded;
                isAdded = bl.AddCategory(Category);
                if (isAdded)
                {
                    lblAdded.Visible = true;
                    Response.Redirect("CategoriesList.aspx", true);
                }
            }
        }
    }
}